package com.stylefeng.guns.modular.utils;

import org.apache.commons.io.FileUtils;

import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;

/**
 * @author lmshe
 * @create 2018-07-09 15:32
 * @desc
 */
public class ModifyCsproj {
    /**
     * 修改相关配置
     * @param path
     * @param changeContent
     * @param key
     */
    public static void modifyCsproj(String path,String changeContent,String key){
        try {
            File file = new File(path);
            String fileContent = FileUtils.readFileToString(file,"UTF-8");
            byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
            String res = new String(uft8bom,"UTF-8");
            if(!fileContent.contains(res)){
                fileContent = res + fileContent;
            }
            if(!fileContent.contains(key)){
                //切分内容
                String first = fileContent.substring(0,fileContent.indexOf("<Compile"));
                String second = fileContent.substring(fileContent.indexOf("<Compile"));
                StringBuffer sb = new StringBuffer();
                sb.append(first);
                sb.append(changeContent);
                sb.append("    " + second);
                FileUtils.writeStringToFile(file, sb.toString(),"UTF-8");
                System.out.println(path + "修改成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void modifyCsprojContent(String path,String changeContent,String key){
        try {
            File file = new File(path);
            String fileContent = FileUtils.readFileToString(file,"UTF-8");
            byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
            String res = new String(uft8bom,"UTF-8");
            if(!fileContent.contains(res)){
                fileContent = res + fileContent;
            }
            if(!fileContent.contains(key)){
                //切分内容
                String first = fileContent.substring(0,fileContent.indexOf("<Content"));
                String second = fileContent.substring(fileContent.indexOf("<Content"));
                StringBuffer sb = new StringBuffer();
                sb.append(first);
                sb.append(changeContent);
                sb.append("    " + second);
                FileUtils.writeStringToFile(file, sb.toString(),"UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String path = "E:\\myself\\project\\park_management\\SP.Services.Impl\\SP.Services.Impl.csproj";
        String changeContent = "<Compile Include=\"DocumentsService.cs\" />\r\n";
        String key = "DocumentsService.cs";
        modifyCsproj(path,changeContent,key);
    }
}
